//
//  WarnBaseViewController.m
//  Rail
//
//  Created by hychen on 2018/3/14.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "WarnBaseViewController.h"
#import "InformationTableViewCell.h"
#import "WarnDetailViewController.h"
@interface WarnBaseViewController ()
@property (nonatomic, copy) NSString *processResult;
@property (nonatomic, copy) NSString *eventLevel;
@property (nonatomic, copy) NSString *alertType;
@end

@implementation WarnBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isOpenFooter = YES;
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setType:(WarnRequestDataType)type {
    _type = type;
    NSString *title = @"";
    switch (type) {
        case WarnRequestDataTypeNormal:
            title = @"普通报警";
            break;
        case WarnRequestDataTypeWarn:
            title = @"警告报警";
            break;
        case WarnRequestDataTypeSerivous:
            title = @"严重报警";
            break;
        case WarnRequestDataTypeHeavy:
            title = @"重大报警";
            break;
        case WarnRequestDataTypeTodo:
            title = @"未处理报警";
            break;
        case WarnRequestDataTypeDone:
            title = @"已处理报警";
            break;
        case WarnRequestDataTypeUserWatch:
            title = @"人员巡检报警";
            break;
        case WarnRequestDataTypeElectronFence:
            title = @"电子围栏报警";
            break;
        case WarnRequestDataTypeSOS:
            title = @"SOS报警";
            break;
        case WarnRequestDataTypeCutdown:
            title = @"剪断报警";
            break;
        case WarnRequestDataTypeDisappear:
            title = @"消失报警";
            break;
        case WarnRequestDataTypeElectronFenceDone:
            title = @"电子围栏报警消除";
            break;
        case WarnRequestDataTypeCutdownDone:
            title = @"剪断报警消除";
            break;
        case WarnRequestDataTypeLowBattery:
            title = @"低电量报警";
            break;
        default:
            break;
    }
    
    //0普通 1警告 2严重 3重大
    //0待处理 1已处理
    self.processResult = @"";
    self.eventLevel = @"";
    self.alertType = @"";
    switch (self.type) {
        case WarnRequestDataTypeNormal:
            self.eventLevel = @"0";
            break;
        case WarnRequestDataTypeWarn:
            self.eventLevel = @"1";
            break;
        case WarnRequestDataTypeSerivous:
            self.eventLevel = @"2";
            break;
        case WarnRequestDataTypeHeavy:
            self.eventLevel = @"3";
            break;
        case WarnRequestDataTypeTodo:
            self.processResult = @"0";
            break;
        case WarnRequestDataTypeDone:
            self.processResult = @"1";
            break;
        case WarnRequestDataTypeUserWatch:
            self.alertType = @"00";
            break;
        case WarnRequestDataTypeElectronFence:
            self.alertType = @"01";
            break;
        case WarnRequestDataTypeSOS:
            self.alertType = @"02";
            break;
        case WarnRequestDataTypeCutdown:
            self.alertType = @"03";
            break;
        case WarnRequestDataTypeDisappear:
            self.alertType = @"04";
            break;
        case WarnRequestDataTypeElectronFenceDone:
            self.alertType = @"05";
            break;
        case WarnRequestDataTypeCutdownDone:
            self.alertType = @"06";
            break;
        case WarnRequestDataTypeLowBattery:
            self.alertType = @"07";
            break;
        default:
            break;
    }
    [self.navigationItem addSimpleTitleView:title];
}

- (void)refreshData {
    self.currentPage = 1;
    NSString *pageString = [NSString stringWithFormat:@"%ld",self.currentPage];
    BMWeakSelf
    [HttpRequest loadWarnListDataWithStatus:self.processResult alertType:self.alertType eventLevel:self.eventLevel eventId:@"" pageNo:pageString success:^(NSArray *object) {
        weakSelf.dataArray = [NSMutableArray arrayWithArray:object?:@[]];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    NSString *pageString = [NSString stringWithFormat:@"%ld",self.currentPage];
    BMWeakSelf
    [HttpRequest loadWarnListDataWithStatus:self.processResult alertType:self.alertType eventLevel:self.eventLevel eventId:@"" pageNo:pageString success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WarnDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    InformationTableViewCell *cell = [InformationTableViewCell cellWithTableView:tableView class:[InformationTableViewCell class]];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WarnDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    WarnDetailViewController *vc = [WarnDetailViewController new];
    vc.model = model;
    BMWeakSelf
    [vc setBlockProcessedWarn:^{
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
